/**
 * Created by e430c on 2015/1/14.
 */
function init(){
    //$("#game_userid").val("1000111100012204189");
    //$("#gasCardTel").val("18811590723");
    //$("#gasCardName").val("车展中国");

    //判断网络连接
    window.addEventListener("online", function () {

    });
    window.addEventListener("offline", function () {
        alertFade();
        alert("设备已断网！");
    });

    //下拉菜单
    $("#mySelectList li").each(function () {
        $(this).on("click", function () {
            $("#dropdown").html($(this).text() + "&nbsp;&nbsp;<span class=\"caret\"></span>");
            $cardid = $(this).attr("cardid");
            if($cardid == "64127500"){
                $payType = 0;
                $("#cardnum").removeAttr("readonly");
                $("#cardnum").attr("placeholder","请输入");
                $("#cardnum").val("");
                $("#ordercash").val("");
            } else if($cardid == "64157002"){
                $payType = 1;
                $("#cardnum").attr("readonly","readonly");
                $("#cardnum").val(500);
                $("#ordercash").val("490元");
            } else if($cardid == "64157001"){
                $payType = 1;
                $("#cardnum").attr("readonly","readonly");
                $("#cardnum").val(1000);
                $("#ordercash").val("980元");
            }
        });
    });
    $("#cardnum").blur(function () {
        if($(this).val() == "500"){
            $("#ordercash").val("490元");
        } else if($(this).val() == "1000"){
            $("#ordercash").val("980元");
        } else{
            if($(this).val() != ""){
                if(isRightMoney($(this).val()) == false){
                    alert("请输入正确的充值金额","确定",0);
                    $(this).parent().parent().css({"border-color":"red"});
                }else{
                    $(this).parent().parent().css({"border-color":"#B7B2AC"});
                    $("#ordercash").val($(this).val() + "元");
                }
            }else{
                $(this).parent().parent().css({"border-color":"#B7B2AC"});
            }
        }
    });
}

function oilPay(){
    $('#myOilCard').css({'display':'none'});
    shieldFade();
    alert("正在充值,请勿退出");
    $orderid = "S" + new Date().getTime();
    var $temp = "";
    if($payType == 0){
        $temp = "JH6d39f1878f1d783206686db938d2b9d3" + "1b1ce5b40008fc5477e61fa51f0d2ea1" + $cardid + $("#cardnum").val() + $("#game_userid").val() + $orderid;
    } else if($payType == 1){
        $temp = "JH6d39f1878f1d783206686db938d2b9d3" + "1b1ce5b40008fc5477e61fa51f0d2ea1" + $cardid + "1" + $("#game_userid").val() + $orderid;
    }
    $sign = hex_md5($temp);
    var url = "api.php";
    $.post(
        url,
        {
            //第三方接口地址
            urls:"http://op.juhe.cn/ofpay/sinopec/onlineorder",
            //請求第三方接口方式 post get
            method:'post',
            //code参数为json串,串内参数都需要加密
            code:'',
            //其他无需加密参数,参数为json串,串内参数不加密
            other:'{"cardid":"'+$cardid+'","cardnum":"'+$("#cardnum").val()+'","orderid":"'+$orderid+'","game_userid":"'+$("#game_userid").val()+'","gasCardTel":"'+$("#gasCardTel").val()+'","gasCardName":"'+$("#gasCardName").val()+'","key":"1b1ce5b40008fc5477e61fa51f0d2ea1","sign":"'+$sign+'"}',
            keytype: 1
        },
        function (data) {
            var $data = eval("("+data+")");
            setTimeout(function () {
                switch($data.error_code){
                    case 0:
                        //设置查询充值延时
                        var timeOut = 0;
                        $oilPayResult = setInterval(function () {
                            timeOut = timeOut + 1;
                            if(timeOut == 360){
                                window.clearInterval($oilPayResult);
                                alertFade();
                                alert("充值超时！","",1);
                            } else {
                                getOilPayResult();
                            }
                        },2000);
                        break;
                    case 208701:
                        alertFade();
                        alert($data.reason,"确定",0);
                        break;
                    case 208702:
                        alertFade();
                        alert($data.reason,"确定",0);
                        break;
                    case 208707:
                        alertFade();
                        alert($data.reason,"确定",0);
                        break;
                    case 208708:
                        alertFade();
                        alert($data.reason,"确定",0);
                        break;
                    case 208710:
                        alertFade();
                        alert($data.reason,"确定",0);
                        break;
                    case 208714:
                        alertFade();
                        alert($data.reason,"确定",0);
                        break;
                    case 208706:
                        alertFade();
                        alert($data.reason,"确定",0);
                        break;
                }
            },2000);
        }
    );
}

function getOilPayResult(){
    var url = "api.php";
    $.post(
        url,
        {
            //第三方接口地址
            urls:"http://op.juhe.cn/ofpay/sinopec/query",
            //請求第三方接口方式 post get
            method:'post',
            //code参数为json串,串内参数都需要加密
            code:'',
            //其他无需加密参数,参数为json串,串内参数不加密
            other:'{"orderid":"'+$orderid+'","key":"1b1ce5b40008fc5477e61fa51f0d2ea1"}',
            keytype: 1
        },
        function (data) {
            var $data = eval("("+data+")");
            if($data.error_code == 0){
                window.clearInterval($oilPayResult);
                alertFade();
                alert("充值成功！","",1);
            }
        }
    );
}

//检测加油卡号是否有效
function checkOilCard(){
    if($("#mySelect span").attr("value") == 0 || $("#cardnum").val() == "" || $("#game_userid").val() == "" || $("#gasCardTel").val() == "" || $("#gasCardName").val() == ""){
        alert("有信息未填写！","确定",0);
    } else if(isRightMoney($("#cardnum").val()) == false){
        alert("请输入正确的充值金额","确定",0);
    }  else if(isRightName($("#gasCardName").val()) == false){
        alert("请输入正确的姓名","确定",0);
    }else if(isRightTel($("#gasCardTel").val()) == false){
        alert("请输入正确的电话号码","确定",0);
    } else{
        alert("正在检测卡号信息...");
        var $sign = hex_md5("JH6d39f1878f1d783206686db938d2b9d3" + "1b1ce5b40008fc5477e61fa51f0d2ea1" + $("#game_userid").val());
        var url = "api.php";
        $.post(
            url,
            {
                //第三方接口地址
                urls:"http://op.juhe.cn/ofpay/sinopec/querycard",
                //請求第三方接口方式 post get
                method:'post',
                //code参数为json串,串内参数都需要加密
                code:'',
                //其他无需加密参数,参数为json串,串内参数不加密
                other:'{"key":"1b1ce5b40008fc5477e61fa51f0d2ea1","game_userid":"'+$("#game_userid").val()+'","sign":"'+$sign+'"}',
                keytype: 1
            },
            function (data) {
                var $data = eval("("+data+")");
                switch ($data.error_code){
                    case 0:
                        alertFade();
                        //oilPay();
                        $("#oilCardName").text($data.result.username);
                        $("#oilCardNum").text($data.result.game_userid);
                        shield();
                        $("#myOilCard").css({'display':'block'});
                        break;
                    case 208711:
                        alertFade();
                        alert($data.reason,"确定",0);
                        break;
                }
            }
        );
    }
}